package com.xzakota.minesweeper.ui.dialog;

import com.xzakota.minesweeper.contant.ConstantPool;
import com.xzakota.minesweeper.ui.dialog.templae.InputDialog;
import com.xzakota.minesweeper.util.JSONFileUtils;
import com.xzakota.minesweeper.util.StringUtils;

import java.awt.*;

/**
 * IP 设置对话框
 *
 * @author Xzakota
 */
public class IPDialog extends InputDialog {
    public IPDialog() {
        setTitle("请输入服务端 IP 地址");
        setMessage("账号登录、注册，以及进行多人游玩\n都需要设置目标服务端 IP 地址\n");
        setMinimumSize(new Dimension(300, 200));

        setTagText("服务端 IP 地址: ");
        setFieldText(JSONFileUtils.getString(ConstantPool.SETTINGS_SERVER_IP));
        removeConfirmAllAction();
        setConfirmAction(e -> {
            setHintText("");

            if (StringUtils.isInvalid(getFieldText(), StringUtils.IP_REGEX_PATTERN)) {
                setHintText("IP 地址不合法", Color.RED);

                return;
            }

            JSONFileUtils.put(ConstantPool.SETTINGS_SERVER_IP, getFieldText(), true);

            closeDialogAndSetResult(true);
        });
    }

    @Override
    public void showWindow() {
        requestFocus();
        super.showWindow();
    }

    @Override
    public Object showWindow2() {
        requestFocus();
        return super.showWindow2();
    }
}
